#!/bin/bash
#pMusic - build gtkdialog xml code
#GPL - Copyright Sigmund Berglund

export TEXTDOMAIN=pmusic
export OUTPUT_CHARSET=UTF-8
[ ! -s $WORKDIR/filebrowser_dir ] && echo $HOME > $WORKDIR/filebrowser_dir
read BROWSER_DIR <$WORKDIR/filebrowser_dir
echo 0 > $WORKDIR/tab_search #default tab in <notebook>

searchfield(){
echo '
<vbox>
 <hbox spacing="1">
  <button '$SF' tooltip-text="'$(gettext 'Favorites')'">
   <input file stock="gtk-favorites"></input>
   <action signal="button-press-event">clear:SOURCE</action>
   <action>'$APPDIR'/func_favorites -show_favorites</action> 
   <action>refresh:SOURCE</action>
   <action type="activate">ALBUMART_SEARCH_ACTIVATE</action>
  </button>
  <togglebutton '$SF' tooltip-text="'$(gettext 'Define which sources to search')'">
   <variable>VIEW_SEARCH_OPTIONS</variable>
   <input file stock="gtk-find_more"></input>
   <default>'$VIEW_SEARCH_OPTIONS'</default>
   <action>if true show:VBOX_SEARCH_OPTIONS</action>
   <action>if false hide:VBOX_SEARCH_OPTIONS</action>
  </togglebutton>
  <togglebutton '$SF' tooltip-text="'$(gettext 'Smart add')'">
   <variable>VIEW_ADD_OPTIONS</variable>
   <input file stock="gtk-add_more"></input>
   <default>'$VIEW_ADD_OPTIONS'</default>
   <action>if true show:VBOX_ADD_OPTIONS</action>
   <action>if false hide:VBOX_ADD_OPTIONS</action>
  </togglebutton>
  <button relief="2" '$SF' tooltip-text="'$(gettext 'Go back in source history')'">
   <variable>HISTORY_UNDO</variable>
   <input file stock="gtk-pmusic_undo"></input>
   <height>20</height>
   <action>'$APPDIR'/func -history_undo</action>
   <action>refresh:SEARCH</action>
   <action>refresh:SOURCE</action>
   <action type="activate">ALBUMART_SEARCH_ACTIVATE</action>
  </button>
  <button relief="2" '$SF' tooltip-text="'$(gettext 'Go forward in source history')'">
   <variable>HISTORY_REDO</variable>
   <input file stock="gtk-pmusic_redo"></input>
   <action>'$APPDIR'/func -history_redo</action>
   <action>refresh:SEARCH</action>
   <action>refresh:SOURCE</action>
   <action type="activate">ALBUMART_SEARCH_ACTIVATE</action>
  </button>
  <entry name="search" height-request="30" activates-default="true" is-focus="true" secondary-icon-stock="gtk-find" tooltip-markup="'"$(gettext "<b>Free-search</b>
  <i>'nig tnt'</i> finds 'TNT - Desperate night'
  <i>'nig -tnt'</i> does NOT find any TNT files 
<b>Add files and URLs</b> to playqueue
<b>Browse paths</b>")"'" '$ST'>
   <variable>SEARCH</variable>
   <input file>'$WORKDIR'/filebrowser_dir</input>
   <action signal="activate">'$APPDIR'/func -search_refresh</action>
   <action signal="activate">refresh:SOURCE</action>
   <action signal="activate" type="activate">ALBUMART_SEARCH_ACTIVATE</action>
###   <action signal="activate" function="break">""</action>
   <action signal="secondary-icon-release">'$APPDIR'/func -search_refresh</action>
   <action signal="secondary-icon-release">refresh:SOURCE</action>
   <action signal="secondary-icon-release" type="activate">ALBUMART_SEARCH_ACTIVATE</action>
   <action condition="command_is_true([[ `echo \"$SEARCH\" | grep -F \"'$(gettext 'Search for music')'\"` ]] && echo true)" signal="enter-notify-event">clear:SEARCH</action>'
   [ $SEARCH_WHILE_TYPING = true ] && echo '<action signal="key-release-event">'$APPDIR'/func -search &</action>'
  echo '</entry>
 </hbox>

 <vbox visible="'$VIEW_SEARCH_OPTIONS'" '$ST'>
 <hseparator></hseparator>
 <hbox spacing="5" tooltip-text="'$(gettext 'Define which sources to search')'" '$ST'>
  <button '$SF'>
   <input file stock="gtk-find"></input>
   <action>'$APPDIR'/func -search_refresh</action>
   <action>refresh:SOURCE</action>
  </button>
  <text width-request="5" '$SF'><label>""</label></text>
  <vbox spacing="2" '$SF'>
   <checkbox label="'$(gettext 'My tracks')'" height-request="17" tooltip-text="'$(gettext 'Search local files inside the pMusic database')'">
    <variable>SEARCH_MYMUSIC</variable>
    <input>echo $SEARCH_MYMUSIC</input>'
    [ ! -f "$STORAGE_DIR/index_mymusic" ] && echo '<sensitive>false</sensitive>'
    echo '
   </checkbox>
   <checkbox label="'$(gettext 'Collections')'" height-request="17" tooltip-text="'$(gettext 'This includes both user-saved playlist files and album-list generated by pMusic')'">
    <variable>SEARCH_COLLECTIONS</variable>
    <input>echo $SEARCH_COLLECTIONS</input>'
    if [ ! -f "$STORAGE_DIR/index_mymusic" ]; then echo '<sensitive>false</sensitive>'; fi
    echo '
   </checkbox>
  </vbox>
  <vbox spacing="2" '$SF'>
   <checkbox label="'$(gettext 'Radio')'" height-request="17" tooltip-text="'$(gettext 'Search radio stations')'">
    <variable>SEARCH_RADIO</variable>
    <input>echo $SEARCH_RADIO</input>
   </checkbox>
   <checkbox label="'$(gettext 'Podcasts')'" height-request="17" tooltip-text="'$(gettext 'Search podcast channels')'" '$ST'>
    <variable>SEARCH_PODCAST</variable>
    <input>echo $SEARCH_PODCAST</input>
   </checkbox>
  </vbox>
  <vbox spacing="2" '$SF'>
   <checkbox label="Youtube" height-request="17" tooltip-text="'$(gettext 'Searching youtube.com requires internet connection')'" '$SF'>
    <variable>SEARCH_YOUTUBE</variable>
    <input>echo $SEARCH_YOUTUBE</input>
   </checkbox>
   <checkbox label="Jamendo" height-request="17" tooltip-text="'$(gettext 'Searching jamendo.com requires internet connection')'" '$SF'>
    <variable>SEARCH_JAMENDO</variable>
    <input>echo $SEARCH_JAMENDO</input>
   </checkbox>
  </vbox>
  <vbox spacing="2" '$ST'>
   <hbox '$SF'>
    <checkbox label="'$(gettext 'Album art')'" tooltip-text="'$(gettext 'Search downloaded image files in the album-art storage (activate in the preferences)')': '$STORAGE_DIR'/albumart" height-request="17">
     <variable>SEARCH_ALBUMART</variable>
     <input>echo $SEARCH_ALBUMART</input>
     <action>if true show:ALBUMART_SEARCH</action>
     <action>if false hide:ALBUMART_SEARCH</action>'
     [[ "$*" = *skip_albumart* ]] && echo '<sensitive>false</sensitive>'
    echo '</checkbox>
    <checkbox label="'$(gettext 'Lyrics')'" height-request="17" tooltip-text="'$(gettext 'Search inside the downloaded lyrics files. Note that this does not support a free search. If searching several words, they have to match perfectly in the lyrics.')'" '$SF'>
     <variable>SEARCH_LYRICS</variable>
     <input>echo $SEARCH_LYRICS</input>
    </checkbox>
    <text '$ST'><label>""</label></text>
   </hbox>
   <hbox spacing="0" tooltip-text="'$(gettext 'Search local files without looking in the pMusic database - convensional search')'" '$ST'>
    <checkbox label="" height-request="17" '$SF'>
     <variable>SEARCH_FILES</variable>'
     ! type pfilesearch > /dev/null 2>&1 && echo '<sensitive>false</sensitive>'
     echo '<input>echo $SEARCH_FILES</input>
    </checkbox>
    <entry height-request="17" '$ST'>
     <variable>SEARCHPATH</variable>
     <default>"'$SEARCHPATH'"</default>'
     ! type pfilesearch > /dev/null 2>&1 && echo '<sensitive>false</sensitive>'
    echo '
    </entry>
   </hbox>
  </vbox>
 </hbox>
 <variable>VBOX_SEARCH_OPTIONS</variable>
 </vbox>
 
 <vbox visible="'$VIEW_ADD_OPTIONS'" '$ST'>
 <hseparator></hseparator>
 <hbox spacing="5" tooltip-text="'$(gettext 'Add tracks based on request')'" '$ST'>
  <button name="highlight" tooltip-text="'$(gettext 'Activate Smart Add')'" '$SF'>
   <input file stock="gtk-pmusic_add"></input>
   ###now save the tree in case sort order has been changed. as seen below we have to rearrange since gtkdialog always execute icons in col2.
   <action>save:SOURCE</action>
   <action>cut -d"|" -f3-16 '$WORKDIR'/sourcelist_output > '$WORKDIR'/tmp_sourcelist1</action>
   <action>cut -d"|" -f2 '$WORKDIR'/sourcelist_output > '$WORKDIR'/tmp_sourcelist2</action>
   <action>cut -d"|" -f18- '$WORKDIR'/sourcelist_output > '$WORKDIR'/tmp_sourcelist3</action>
   <action>paste -d "|" '$WORKDIR'/tmp_sourcelist1 '$WORKDIR'/tmp_sourcelist2 '$WORKDIR'/tmp_sourcelist3 > '$WORKDIR'/sourcelist</action>
   ###---
   <action>'$APPDIR'/func_add -smartadd $SMARTADD_NR $SMARTADD_RANDOM "$SMARTADD_SOURCE" &</action>
   <action>refresh:PLAYLIST</action>
  </button>
  <text width-request="5" '$SF'><label>""</label></text>
  <vbox spacing="2">
   <radiobutton height-request="17" '$SF'>
    <variable>SMARTADD_SORT_ORDER</variable>
    <label>'$(gettext 'Sort order')'</label>
   </radiobutton>
   <radiobutton height-request="17" '$SF'>
    <variable>SMARTADD_RANDOM</variable>
    <input>echo '$SMARTADD_RANDOM'</input>
    <label>'$(gettext 'Random')'</label>
   </radiobutton>
  </vbox>
  <vbox '$SF' tooltip-text="'$(gettext 'How many tracks should be added to playqueue')'">
   <comboboxentry width-request="65" '$ST'>
    <variable>SMARTADD_NR</variable>
    <default>"'$SMARTADD_NR'"</default>
    <input>echo -e "'$(gettext 'All')'\n5\n10\n20\n30\n50\n100"</input>
   </comboboxentry>
  </vbox>
  <text '$SF' width-request="10"><label>""</label></text>
  <text '$SF'><label>"'$(gettext 'Source')'"</label></text>
  <vbox homogeneous="true" '$SF'>
   <comboboxtext '$SF'>
    <variable>SMARTADD_SOURCE</variable>
    <input>echo -e "'$SMARTADD_SOURCE'\n'$(gettext 'List below')'\n'$(gettext 'My music (DB)')'"</input>
   </comboboxtext>
  </vbox>
  <text '$SF' width-request="10"><label>""</label></text>
  <text '$SF'><label>"'$(gettext 'Filters')'"</label></text>
  <button '$SF' tooltip-text="'$(gettext 'How many times should the selected tracks have been played before')'">
   <variable>BUTTON_SMARTADD_RATING</variable>
   <height>22</height>
   <input file>'$WORKDIR'/smartadd_rating.svg</input>
   <action signal="button-press-event">. '$APPDIR'/menu_smartadd</action>
   <action signal="button-release-event">refresh:BUTTON_SMARTADD_RATING</action>
   <action signal="button-release-event">refresh:BUTTON_SMARTADD_BPM</action>
   <action signal="button-release-event">refresh:BUTTON_SMARTADD_GENRE</action>
  </button>
  <button '$SF' tooltip-text="'$(gettext 'How many beats per minute should the selected tracks have')'">
   <variable>BUTTON_SMARTADD_BPM</variable>
   <height>22</height>
   <input file>'$WORKDIR'/smartadd_tempo.svg</input>
   <action signal="button-press-event">. '$APPDIR'/menu_smartadd</action>
   <action signal="button-release-event">refresh:BUTTON_SMARTADD_RATING</action>
   <action signal="button-release-event">refresh:BUTTON_SMARTADD_BPM</action>
   <action signal="button-release-event">refresh:BUTTON_SMARTADD_GENRE</action>
  </button>
  <button '$SF' tooltip-text="'$(gettext 'What genre should the selected tracks be')'">
   <variable>BUTTON_SMARTADD_GENRE</variable>
   <height>22</height>
   <input file>'$WORKDIR'/smartadd_genre.svg</input>
   <action signal="button-press-event">. '$APPDIR'/menu_smartadd</action>
   <action signal="button-release-event">refresh:BUTTON_SMARTADD_RATING</action>
   <action signal="button-release-event">refresh:BUTTON_SMARTADD_BPM</action>
   <action signal="button-release-event">refresh:BUTTON_SMARTADD_GENRE</action>
  </button>
  <text '$ST'><label>""</label></text>
 </hbox>
 <variable>VBOX_ADD_OPTIONS</variable>
 </vbox>
</vbox>'
}


GUI_ADD_SEARCH="$(searchfield $*)"

#calculate size of albumart depending on window size
#  <tree> widget is static 300px
#  playqueue tabs is 35px
#  2 arrow buttons pluss spacing is 2x40px
#  general spacing is 3x5px
#  pluss 30 ???
read TMP < $WORKDIR/PLAYLIST_MULTIPLE
[ $TMP = true ] && PLAYQUEUE_WIDTH=460 || PLAYQUEUE_WIDTH=428
ALBUMART_SPACE="$(($WINDOW_WIDTH-$PLAYQUEUE_WIDTH))"

if [[ "$*" != *skip_albumart* ]]; then
	read DONT_SAVE_FRONTEND < $WORKDIR/DONT_SAVE_FRONTEND
	export ALBUMART_SEARCH_NR=$(($ALBUMART_SPACE/($ALBUMART_SEARCH_HEIGHT+7))) #pluss spacing
	[ ! "$ALBUMART_SEARCH_NR" ] && ALBUMART_SEARCH_NR=6
	ALBUMART_SEARCH_HEIGHT2=$((($ALBUMART_SPACE-($ALBUMART_SEARCH_NR*7))/$ALBUMART_SEARCH_NR)) #$NR*7 is spacing between images
	[ ! "$ALBUMART_SEARCH_HEIGHT2" ] && ALBUMART_SEARCH_HEIGHT2=70
	[ "$DONT_SAVE_FRONTEND" != "true" ] && echo $ALBUMART_SEARCH_NR > $WORKDIR/albumart_search_NR
	#
	XML_ALBUMART_SEARCH='
	<vbox height="'$(($ALBUMART_SEARCH_HEIGHT2+15))'" spacing="0" margin="0" '$SF'>
	 <eventbox name="search_albumart" above-child="false" visible-window="true" '$ST'>
	  <vbox scrollable="true" shadow-type="0" height="'$(($ALBUMART_SEARCH_HEIGHT2+15))'" hscrollbar-policy="2" vscrollbar-policy="2" spacing="0" margin="0" '$SF'>
	   <eventbox name="search_albumart" above-child="false" visible-window="true" '$ST'>
	    <hbox spacing="7" '$ST'>
	     <eventbox name="search_albumart" above-child="true" visible-window="true" '$SF'>
	      <pixmap>
	       <variable>ALBUMART_SEARCH_PREV</variable>
	       <input file>'$WORKDIR'/albumart_search_prev.svg</input>
	       <width>30</width>
	      </pixmap>
	      <action signal="button-press-event">'$APPDIR'/func -search_albumart_update prev</action>
	      <action signal="button-press-event" type="activate">ALBUMART_SEARCH_ACTIVATE</action>
	     </eventbox>'

	#build images 
	for I in $(seq 1 $ALBUMART_SEARCH_NR); do
		XML_ALBUMART_SEARCH=$XML_ALBUMART_SEARCH'
		<eventbox name="search_albumart" above-child="true" visible-window="true" spacing="0" '$SF'>
		 <pixmap>
		  <variable>ALBUMART_SEARCH'$I'</variable>
		  <input file>'$WORKDIR'/albumart_search'$I'.jpg</input>
		  <width>'$ALBUMART_SEARCH_HEIGHT2'</width>
		  <height>'$ALBUMART_SEARCH_HEIGHT2'</height>
		 </pixmap>
		 <action signal="enter-notify-event">TMP="`readlink '$WORKDIR'/albumart_search'$I'.jpg`"; if [ "$TMP" ]; then TMP=${TMP##*/}; echo "${TMP/.jpg/}" > '$WORKDIR'/filebrowser_dir; fi</action>
		 <action signal="enter-notify-event">refresh:SEARCH</action>
		 <action signal="button-press-event">. '$APPDIR'/menu_albumart_search</action>
		 <action signal="button-release-event">refresh:SOURCE</action>
		</eventbox>'
	done

	XML_ALBUMART_SEARCH=$XML_ALBUMART_SEARCH'
	     <eventbox name="search_albumart" above-child="true" visible-window="true" '$SF'>
	      <pixmap>
	       <variable>ALBUMART_SEARCH_NEXT</variable>
	       <input file>'$WORKDIR'/albumart_search_next.svg</input>
	       <width>30</width>
	      </pixmap>
	      <action signal="button-press-event">'$APPDIR'/func -search_albumart_update next</action>
	      <action signal="button-press-event" type="activate">ALBUMART_SEARCH_ACTIVATE</action>
	     </eventbox>
	     <text '$ST'><label>""</label></text>
	    </hbox>
	   </eventbox>
	  </vbox>
	  <action signal="enter-notify-event">cp -f '$WORKDIR'/filebrowser_dir '$WORKDIR'/tmp_filebrowser_dir</action>
	  <action signal="leave-notify-event">cp -f '$WORKDIR'/tmp_filebrowser_dir '$WORKDIR'/filebrowser_dir</action>
	  <action signal="leave-notify-event">refresh:SEARCH</action>
	  ### <action signal="button-press-event">. '$APPDIR'/menu_albumart_search disabled</action>
	 </eventbox>
	 <variable>ALBUMART_SEARCH</variable>
	</vbox>'
	
	#search once again in case gui is scaled up and we should show more images
	[ "$DONT_SAVE_FRONTEND" != true ] && $APPDIR/func -search_albumart_update
fi


GUI_ADD_LIST='
<tree name="sourcelist" rubber-banding="true" fixed-height-mode="true" selection-mode="3" sort-function="1" exported_column="0" '$ST' column-sort-function="0|0|0|0|1|1|0|1|1|0|1|1|1|0" column-sizing="22|200|150|150|45|60|90||60|80|60|70|55|" column-resizeable="false|true|" column-header-active="false|true|" column-visible="true|'$COLUMN_ARTIST'|'$COLUMN_TITLE'|'$COLUMN_ALBUM'|'$COLUMN_NR'|'$COLUMN_YEAR'|'$COLUMN_GENRE'|'$COLUMN_COMMENT'|'$COLUMN_RATING'|'$COLUMN_FORMAT'|'$COLUMN_BITRATE'|'$COLUMN_LENGTH'|'$COLUMN_TEMPO'|'$COLUMN_PATH'|false|false|false|false|false|false|false">
 <variable>SOURCE</variable>
 <height>100</height><width>300</width>
 <label>"|'$(gettext 'Artist / Filename')'                             |'$(gettext 'Title')'                             |'$(gettext 'Album')'                     |'$(gettext 'Nr')'  |'$(gettext 'Date')'     |'$(gettext 'Genre ')'     |'$(gettext 'Comment')'  |'$(gettext 'Rate')'|'$(gettext 'Format')'|'$(gettext 'Kb/s')'  |'$(gettext 'Length')'|'$(gettext 'bpm')'|'$(gettext 'Path')'                                                                                                                                                                                              ||disc-id musicbrainz.org|||||timestamps"</label>
 <input file stock-column="14">'$WORKDIR'/sourcelist</input>
 <output file>'$WORKDIR'/sourcelist_output</output>
 <action signal="button-press-event">if [ $PTR_BTN -eq 3 ]; then echo "$SOURCE" > '$WORKDIR'/SOURCE_PRESS_EVENT; . '$APPDIR'/menu_sourcelist; fi</action>
 <action signal="button-release-event">if [ $PTR_BTN -eq 2 ]; then '$APPDIR'/func -play_direct "$SOURCE"; fi</action>
 <action>'$APPDIR'/func -browse</action>
 <action>'$APPDIR'/func_add -add &</action>
 <action>refresh:UPDATE_SOURCELIST</action> ###<checkbox> that is TRUE if sourcelist should update, at FALSE - playlist will update
</tree>
'

GUI_ALBUMART_SEARCH
GUI_ADD_FIELD='
<vbox spacing="0" '$ST'>
 '$XML_ALBUMART_SEARCH'
 <vbox '$ST'>
  '$GUI_ADD_LIST'
 </vbox>
</vbox>
'
